package com.cincc.cinccimdemo.ui.conversation.sharefile;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.TimeUtils;
import com.cincc.im.table.IMGroupFile;

import java.util.ArrayList;
import java.util.List;

public class GroupShareFileAdapter extends RecyclerView.Adapter<GroupShareFileAdapter.MyViewHolder> {

    Activity mContext;
    List<IMGroupFile> list = new ArrayList<>();

    public GroupShareFileAdapter(Activity mContext) {
        this.mContext = mContext;
    }

    public void setGroupShareFileList(List<IMGroupFile> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.group_share_file_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        IMGroupFile item = list.get(position);

        holder.nameText.setText(item.uploadUserName);
        holder.timeTv.setText(TimeUtils.getMsgFormatTime1(item.updateTime));
        holder.fileNameTv.setText(item.fileName);
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ((GroupShareFileActivity) mContext).delGroupShareFile(item.fileId);
                return false;
            }
        });
    }

    public IMGroupFile getItem(int index) {
        return list.get(index);
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView firstTv;
        ImageView avatar;
        TextView nameText;
        TextView timeTv;
        TextView fileNameTv;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            firstTv = itemView.findViewById(R.id.first_letter_tv);
            avatar = itemView.findViewById(R.id.img_avatar);

            nameText = itemView.findViewById(R.id.upload_user_name_tv);
            timeTv = itemView.findViewById(R.id.upload_time_tv);
            fileNameTv = itemView.findViewById(R.id.file_name_tv);
        }
    }
}
